class Game {
    constructor() {
        this.deck = new Deck();
        this.players = [
            new Player('玩家', false),
            new Player('电脑1', true),
            new Player('电脑2', true)
        ];
        this.currentPlayerIndex = 0;
        this.lastPlay = [];
        this.ui = new GameUI();
    }

    start() {
        this.deck.shuffle();
        
        // 发牌
        this.players.forEach(player => {
            player.addCards(this.deck.deal(17));
        });

        // 发地主牌
        const landlordCards = this.deck.deal(3);
        this.players[0].addCards(landlordCards);  // 简化版本：玩家永远是地主
        this.players[0].isLandlord = true;

        this.currentPlayerIndex = 0;
        this.ui.renderPlayerCards(this.players[0].cards);
    }

    get currentPlayer() {
        return this.players[this.currentPlayerIndex];
    }

    validatePlay(cards) {
        return Rules.canBeat(cards, this.lastPlay);
    }

    playCards(cards) {
        this.currentPlayer.removeCards(cards);
        this.lastPlay = cards;
        this.ui.renderPlayedCards(cards);
        this.ui.renderPlayerCards(this.currentPlayer.cards);
        
        if (this.currentPlayer.cards.length === 0) {
            this.gameOver();
            return;
        }

        this.nextTurn();
    }

    pass() {
        if (this.lastPlay.length === 0) {
            alert('第一手不能过牌！');
            return;
        }
        this.nextTurn();
    }

    nextTurn() {
        this.currentPlayerIndex = (this.currentPlayerIndex + 1) % 3;
        if (this.currentPlayer.isBot) {
            setTimeout(() => this.botPlay(), 1000);
        }
    }

    botPlay() {
        const move = this.currentPlayer.makeMove(this.lastPlay);
        if (move.length > 0) {
            this.playCards(move);
        } else {
            this.pass();
        }
    }

    gameOver() {
        const winner = this.currentPlayer;
        alert(`游戏结束！${winner.name}获胜！`);
    }
}

// 创建游戏实例
const game = new Game(); 